package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.user.models.bo.CustomerInfoBO;
import com.yt.user.models.bo.CustomerInfoSaveBO;
import com.yt.user.models.dto.CustomerInfoDTO;
import com.yt.user.models.dto.LoginInfoDTO;
import com.yt.user.models.service.ICustomerInfoService;
import com.yt.user.models.service.ILoginInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 客户信息控制器
 *
 * @author zhou
 * @Date 2025-09-08 15:43:37
 */

@RestController
@RequestMapping("/api/customerInfo")
@Slf4j
@Api(value = "CustomerInfoApiController", tags = "客户信息Api接口")
public class CustomerInfoApiController {

    @DubboReference
    private ICustomerInfoService customerInfoService;
    @DubboReference
    private ILoginInfoService loginInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<CustomerInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        CustomerInfoDTO dto = customerInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "客户信息列表", notes = "客户信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<CustomerInfoDTO>> queryPageList(CustomerInfoBO bo) {
        Wrapper<CustomerInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<CustomerInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = customerInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public Result<Boolean> register(@RequestBody CustomerInfoSaveBO saveBO) {
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //判断手机号是否已经被注册
        Wrapper<LoginInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account", saveBO.getPhone());
        LoginInfoDTO dto = loginInfoService.selectOne(wrapper);
        if (dto != null) {
            return new Result<>(CodeEnum.FAIL_AddUser_3004);
        }
        //增加客户信息和登录信息
        boolean flag = customerInfoService.addCus(saveBO);

        return new Result<>(flag);
    }
}
